// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: Buzz Bomber Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

// Aliases
private alias object.value0 : buzzBomber.timer
private alias object.value1 : buzzBomber.startX
private alias object.value2 : buzzBomber.hasShot
private alias object.value3 : buzzBomber.hideOnOffScreen // idk if this is ever actually used
private alias object.value4 : buzzBomber.aniTimer
private alias object.value5 : buzzBomber.shootTimer

private alias object.value38 : player.hitboxLeft
private alias object.value39 : player.hitboxRight
private alias object.value40 : player.hitboxTop
private alias object.value41 : player.hitboxBottom

private alias 0 : BUZZBOMBER_BUZZAROUND
private alias 1 : BUZZBOMBER_IDLEDELAY
private alias 2 : BUZZBOMBER_FOUNDPLAYER
private alias 3 : BUZZBOMBER_SHOOTING
private alias 4 : BUZZBOMBER_HIDDEN

private alias 0 : BUZZBOMBER_ANI_FLY
private alias 1 : BUZZBOMBER_ANI_FLY_IDLE
private alias 2 : BUZZBOMBER_ANI_SHOOT
private alias 3 : BUZZBOMBER_ANI_NONE

// Function declarations
reserve function BuzzBomber_DebugDraw
reserve function BuzzBomber_DebugSpawn

function BuzzBomber_DebugDraw
	DrawSpriteFX(0, FX_FLIP, object.xpos, object.ypos)
end function


function BuzzBomber_DebugSpawn
	CreateTempObject(TypeName[Bee], 0, object.xpos, object.ypos)

	// Get the X part of object direction
	GetBit(temp0, object.direction, 0)

	if temp0 == false
		// Facing left
		object[tempObjectPos].direction = FLIP_NONE
		object[tempObjectPos].xvel 		= -0x40000
	else
		// Facing right
		object[tempObjectPos].direction = FLIP_X
		object[tempObjectPos].xvel 		= 0x40000
	end if
	
end function


event ObjectMain
	switch object.state
	case BUZZBOMBER_BUZZAROUND
		object.priority = PRIORITY_ACTIVE
		if buzzBomber.timer < 127
			buzzBomber.timer++
			object.xpos += object.xvel
		else
			buzzBomber.timer 	= 0
			object.state 		= BUZZBOMBER_IDLEDELAY
			object.animation 	= BUZZBOMBER_ANI_FLY_IDLE
			object.direction 	^= FLIP_X
			FlipSign(object.xvel)
		end if
		break

	case BUZZBOMBER_IDLEDELAY
		if buzzBomber.timer < 59
			buzzBomber.timer++
		else
			buzzBomber.timer 	= 0
			object.state 		= BUZZBOMBER_BUZZAROUND
			object.animation 	= BUZZBOMBER_ANI_FLY
			buzzBomber.hasShot	= false
		end if
		break

	case BUZZBOMBER_FOUNDPLAYER
		if buzzBomber.timer < 10
			buzzBomber.timer++
		else
			buzzBomber.timer = 0
			object.state++
			buzzBomber.shootTimer = 27
			object.animation = BUZZBOMBER_ANI_SHOOT
		end if
		break

	case BUZZBOMBER_SHOOTING
		temp0 = object.xvel
		temp0 /= 2
		object.xpos += temp0
		temp7 = buzzBomber.timer
		if buzzBomber.timer < 127
			buzzBomber.timer++
			if object.outOfBounds == false
			if buzzBomber.timer == buzzBomber.shootTimer
				buzzBomber.shootTimer += 48
				CreateTempObject(TypeName[Bee Bomb], 0, object.xpos, object.ypos)
				object[tempObjectPos].direction = object.direction
				if object.direction == FLIP_NONE
					object[tempObjectPos].xpos += 0x50000
				else
					object[tempObjectPos].xpos -= 0x50000
				end if
				object[tempObjectPos].ypos += 0xA0000
				object[tempObjectPos].yvel = 0x28000
			end if
			end if
		else
			buzzBomber.timer	= temp7
			buzzBomber.timer	+= 64
			object.state 		= BUZZBOMBER_BUZZAROUND
			object.animation 	= BUZZBOMBER_ANI_FLY
			buzzBomber.hasShot	= true
		end if
		break

	case BUZZBOMBER_HIDDEN
		object.animation = BUZZBOMBER_ANI_NONE
		break
	end switch

	if object.outOfBounds == true
		temp0 		= object.xpos
		object.xpos = buzzBomber.startX
		if buzzBomber.hideOnOffScreen == true
			object.state = BUZZBOMBER_HIDDEN
		end if

		if object.outOfBounds == true
			object.xpos = buzzBomber.startX

			GetBit(temp0, object.propertyValue, 0)
			if temp0 == 0
				object.direction = FLIP_NONE
				object.xvel 	 = -0x40000
			else
				object.direction = FLIP_X
				object.xvel 	 = 0x40000
			end if
			buzzBomber.timer	= 0
			object.animation 	= BUZZBOMBER_ANI_FLY
			object.priority 	= PRIORITY_ACTIVE_BOUNDS
			object.state 		= BUZZBOMBER_BUZZAROUND
		else
			object.xpos = temp0
		end if
	end if

	//Player Collisons
	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		if object.state == BUZZBOMBER_BUZZAROUND
			if buzzBomber.hasShot == false
				if object.propertyValue < 2
					BoxCollisionTest(C_TOUCH, object.entityPos, -128, -256, 128, 256, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
				else
					BoxCollisionTest(C_TOUCH, object.entityPos, -72, -256, 72, 256, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
				end if
				if checkResult == true
					if object.xpos > object[currentPlayer].xpos
						object.direction = FLIP_NONE
						if object.xvel > 0
							FlipSign(object.xvel)
						endif
					else
						object.direction = FLIP_X
						if object.xvel < 0
							FlipSign(object.xvel)
						endif
					endif
					object.state 		= BUZZBOMBER_FOUNDPLAYER
					buzzBomber.timer 	= 0
					object.animation 	= BUZZBOMBER_ANI_FLY_IDLE
				end if
			end if
		end if
		if object.state < BUZZBOMBER_HIDDEN
			BoxCollisionTest(C_TOUCH, object.entityPos, -24, -12, 24, 12, currentPlayer, player[currentPlayer].hitboxLeft, player[currentPlayer].hitboxTop, player[currentPlayer].hitboxRight, player[currentPlayer].hitboxBottom)
			if checkResult == true
				CallFunction(PlayerObject_BadnikBreak)
			end if
		end if
		
		checkEqual(object[currentPlayer].state, PlayerObject_Death)
		temp0 = checkResult
		checkEqual(object[currentPlayer].state, PlayerObject_DeathAlt)
		temp0 |= checkResult
		checkEqual(object[currentPlayer].state, PlayerObject_Drown)
		temp0 |= checkResult
		if checkResult == TRUE
			object.state = BUZZBOMBER_BUZZAROUND
		endif
	next


end event


event ObjectDraw

	buzzBomber.aniTimer ++
	if buzzBomber.aniTimer > 5
		object.frame ++
		buzzBomber.aniTimer = 0
	endif
	
	switch object.state
		case BUZZBOMBER_BUZZAROUND
		case BUZZBOMBER_IDLEDELAY
		case BUZZBOMBER_FOUNDPLAYER
		case BUZZBOMBER_HIDDEN

				if object.frame > 1
					object.frame = 0
				endif
				
			break
		case BUZZBOMBER_SHOOTING
					if object.frame < 2
						object.frame = 2
					endif
					if object.frame > 9
						object.frame = 2
					endif
			break
	endswitch
	
	drawSpriteFX(object.frame, FX_FLIP, object.xpos, object.ypos)
	
end event


event ObjectStartup
	CheckCurrentStageFolder("SSZ")
	if checkResult == true
		LoadSpriteSheet("SSZ/Objects.gif")
		
		//idle
		SpriteFrame(-24, -20, 48, 35, 1, 131)
		SpriteFrame(-24, -20, 48, 35, 50, 131)
		
		//drop bomb
		SpriteFrame(-24, -20, 48, 35, 99, 131)
		SpriteFrame(-24, -20, 48, 35, 148, 131)
		SpriteFrame(-24, -20, 48, 35, 197, 131)
		SpriteFrame(-24, -20, 48, 35, 246, 131)
		SpriteFrame(-24, -20, 48, 38, 295, 131)
		SpriteFrame(-24, -20, 48, 35, 344, 131)
		SpriteFrame(-24, -20, 48, 35, 393, 131)
		SpriteFrame(-24, -20, 48, 35, 442, 131)
		
	end if




	// propertyValue info:
	// bit 0 = direction
	// bit 1 = range type (large/small)

	foreach (TypeName[Bee], arrayPos0, ALL_ENTITIES)
		buzzBomber[arrayPos0].startX	= object[arrayPos0].xpos
		object[arrayPos0].inkEffect 	= INK_ALPHA
		object[arrayPos0].alpha 		= 0xC0
		GetBit(temp0, object[arrayPos0].propertyValue, 0)
		if temp0 == 0
			object[arrayPos0].direction = FLIP_NONE
			object[arrayPos0].xvel 		= -0x40000
		else
			object[arrayPos0].direction = FLIP_X
			object[arrayPos0].xvel 		= 0x40000
		end if
	next

	SetTableValue(TypeName[Bee], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(BuzzBomber_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(BuzzBomber_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
	DebugMode_ObjCount++
end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
end event
